package wisents.wicket.form.submitingstrategy;

import pl.rabbitsoftware.EnhancedFormTester;
import wisents.api.FormRepresentation;
import wisents.api.factory.Vendor;
import wisents.wicket.WicketVendor;
import wisents.wicket.form.WicketFormRepresentation;
import wisents.wicket.form.formelements.WicketFormElement;

/**
 * Created by IntelliJ IDEA.
 * User: rabbit
 * Date: Jan 19, 2010
 * Time: 9:19:28 PM
 * To change this template use File | Settings | File Templates.
 */
final class EnhancedWicketFormTesterFactory {

    private EnhancedFormTester formTester;

    EnhancedFormTester createEnhancedFormTester(FormRepresentation formRepresentation) {

        WicketFormRepresentation wicketFormRepresentation = (WicketFormRepresentation) formRepresentation;
        formTester = ((WicketVendor) Vendor.getInstance())
                .getEnhancedTester()
                .form(wicketFormRepresentation.path());
        return fillEnhancedFormTester(wicketFormRepresentation);
    }

    private EnhancedFormTester fillEnhancedFormTester(WicketFormRepresentation wicketFormRepresentation) {

        WicketFormElementConverter converter = new WicketFormElementConverter();

        EnhancedFormTester result = formTester;

        for (WicketFormElement formElement : wicketFormRepresentation.getFormElements()) {
            result = converter.convert(formElement, result);
        }
        return result;
    }

}
